package com.itlhc.controller;

import com.github.pagehelper.PageInfo;
import com.itlhc.pojo.Books;
import com.itlhc.pojo.Page;
import com.itlhc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部书籍
    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list", list);
        return "allbook";
    }

    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }

    //增加书籍
    @RequestMapping("/addBook")
    public String addPaper(Books books) {
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    //更新书籍
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, int id) {
        Books books = bookService.queryBookById(id);
        System.out.println(books);
        model.addAttribute("book",books );
        return "updateBook";
    }
    @RequestMapping("/updateBook")
    public String updateBook(Model model, Books book) {
        System.out.println(book);
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/book/allBook";
    }

    //删除书籍
    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

 /*   *//**
     * @param
     * @return
     * @throws Exception
     *//*
    @RequestMapping("/queryPages")
    public String queryPages(HttpServletRequest request, Model model) throws Exception {
        String pageNow = request.getParameter("pageNow");
        Page page = null;
        List<Books> books = new ArrayList<Books>();
        int totalCount = (int) bookService.getGoodsCount(1);
        if (pageNow != null) {
            page = new Page(totalCount, Integer.parseInt(pageNow));
            books = this.bookService.findPages(page.getStartPos(), page.getPageSize());
        } else {
            page = new Page(totalCount, 1);
            books = this.bookService.findPages(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("books_list", books);
        model.addAttribute("page", page);
        return "allbook";
    }*/

    //分页查询
    @RequestMapping("/queryPages")
    public String index3(@RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "count", defaultValue = "4") int count,
                         Model model) {
        System.out.println(page);
        PageInfo<Books> usersPageInfo = bookService.selectBooksPage(page,count);
        System.out.println(usersPageInfo);
        model.addAttribute("usersPageInfo", usersPageInfo);
        return "allbook";
    }
}